<?php
namespace jy_mshop2fee;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	
	public function checklogin(){
		cfc('core')->checklogin();
		$this->model('member')->register();
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function current_daili_file_types(){
		return str_array($this->setting['daili_file']);
	}
	public function available_areas(){
		$areas=file_get_contents(ROOT_D.'/core/resource/area_town.txt');
		$areas=unserialize($areas);
		return $areas;
	}
	public function get_area_code($area){
		$address=$area['province'];
		if($area['city']){
			$address.=' '.$area['city'];
		}
		if($area['district']){
			$address.=' '.$area['district'];
		}
		if($area['street']){
			$address.=' '.$area['street'];
		}
		$area_code=pdo_getcolumn('core_area',array('address'=>$address),'code');
		if(!$area_code){
			$isup=pdo_count('core_area',array(),'code');
			if($isup==0){
				require ROOT_D.'/update/update_area.php';
			}
		}
		return $area_code;
	}
	public function add_upfee(){
		$end_time=strtotime(date("Y-m",time()));
		//$end_time=time();
		$start_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
		$this->month=date("Y-m",$start_time+200);
		$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$end_time);
		$parameter=array(
			'params'=>$params,
			'limit'=>'1',
			'orderby'=>'id desc',
		);
		$list=$this->get_list('upfee',$parameter,$total);
		if(count($list)>0){
			exi('上月的分红已经派发！','error');
		}
		$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($start_time,$end_time));
		$maxlevel=$this->get('level',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id','level desc');
		if(!$maxlevel){
			exi('未设置任何等级！','error');
		}
		//全部分销商
		$members=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'level_id'=>$maxlevel['id'],'is_maxred'=>1,'is_agent'=>1,'uniacid'=>$_SESSION['uniacid']));
		$allfee=$this->getsum('feechi',$params,'money');
		if($allfee<1){
			exi('奖金池为空！','error');
		}
		$total=count($members);
		if($total<1){
			exi('满足条件的老板为空！','error');
		}
		$fee=$allfee/$total;
		$data=array(
			'allfee'=>$allfee,
			'fee'=>$fee,
			'total'=>$total,
		);
		foreach($members as $item){
			$this->xun_upfee($item,$data);
		}
		return true;
	}
	/*
		循环
	*/
	public function xun_upfee($member=array(),$data=array()){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'mid'=>$data['mid'],
			'allfee'=>$data['allfee'],
			'fee'=>$data['fee'],
			'total'=>$data['total'],
			'month'=>$this->month,
			'type'=>5,
			'status'=>0,
			'createtime'=>TIMESTAMP
		);
		$this->pdo_insert('upfee',$insert);
		$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'merchid'=>$_SESSION['onemerchid'],
					'mid'=>$data['mid'],
					'money'=>$data['fee'],
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>5,
		);
		$this->pdo_insert('commission',$insert);
		return true;
	}
	/*
		奖金池
	*/
	public function create_feechi($order){
		if(!$this->setting['max_red']){
			return false;
		}
		$new_fee=array(
				'uniacid'=>$_SESSION['uniacid'],
				'merchid'=>$_SESSION['onemerchid'],
				'mid'=>$order['mid'],
				'type'=>0,
				'ordersn'=>$order['ordersn'],
				'money'=>$this->setting['max_red'],
				'createtime'=>TIMESTAMP,
				'status'=>0,
				'isadd'=>1,
		);
		$this->insert('feechi',$new_fee);
		return true;
	}
}